﻿: numbers
0xE0 0xA0 0xA0 0xE0  # 0
0x40 0x40 0x40 0x40  # 1
0xE0 0x20 0x80 0xE0  # 2
0xE0 0x20 0x60 0xE0  # 3
0xA0 0xE0 0x20 0x20  # 4
0xE0 0x80 0x20 0xE0  # 5
0xE0 0x80 0xE0 0xE0  # 6
0xE0 0x20 0x20 0x20  # 7
0xE0 0xA0 0xE0 0xE0  # 8
0xE0 0xE0 0x20 0xE0  # 9

: c 0xE0 0x80 0x80 0xE0 
: r 0xE0 0xA0 0xC0 0xA0 
: a 0xE0 0xA0 0xE0 0xA0 
: s 0xE0 0x80 0x20 0xE0 
: h 0xA0 0xA0 0xE0 0xA0 
: bang 0x40 0x40 0x00 0x40 

: score 0x00 0x00

: honk  0x00

: tires 0x01

: achievementcounter 0x00

: dude
	0x1C 0x1C 0x08 0x1C 0x1A 0x18 0x14 0x24 
: dudeshoes
	0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x24 
	0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x24 
: dude2
	0x1C 0x1C 0x08 0x1A 0x1C 0x18 0x14 0x18 
: dudeshoes2
	0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x18 
	0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x18 
: truckback
	0x00 0x00 0xFF 0xFF 0xFF 0xFF 0x66 0x66 
: truckfront
	0x7F 0x7F 0xFF 0xFF 0xFF 0xFF 0x18 0x18 
: placeholder
	0x80 0x80 0x80 0x80 0x80 0x80 0x80 0x80 
: topline
	0xFF 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
: pixel
	0x80 0x00 0x00 0x00 0x00 0x00 0x00 0x00
: mountain
	0x00 0x00 0x18 0x1C 0x3C 0x3E 0x7E 0xFF 
: house
	0xFF 0x99 0x99 0xFF 0xBD 0xA5 0xE7 0xE7 
: church
	0x08 0x1C 0x08 0x7F 0x5D 0x5D 0x77 0x77 
: tree
	0xE9 0x12 0x6C 0x4A 0x1A 0x10 0x10 0x18 
: sadsmile
	0x00 0x24 0x24 0x24 0x00 0x3C 0x42 0x81 
: carback
	0x7F 0xE3 0xE3 0xFF 0xFF 0xC3 0x18 0x18 
: carfront
	0xC0 0x30 0x1C 0xFF 0xFF 0xC3 0x18 0x18 

: car1  0x00 0x00 0x00 0x80 0x80 0x80 0x00 0x00 
: car2  0x00 0x00 0x00 0x80 0x80 0x80 0x00 0x00 
: car3  0x00 0x00 0x80 0x80 0x80 0x00 0x00 0x00 
: car4  0x00 0x00 0x80 0x80 0x80 0x00 0x80 0x80 
: car5  0x00 0x80 0x80 0x80 0x80 0x00 0x80 0x80 
: car6  0x00 0x80 0x00 0x80 0x80 0x00 0x00 0x00 
: car7  0x80 0x00 0x00 0x80 0x80 0x80 0x00 0x00 
: car8  0x80 0x00 0x00 0x80 0x80 0x80 0x00 0x00 
: car9  0x80 0x80 0x80 0x80 0x80 0x80 0x00 0x00 
: car10 0x80 0x80 0x80 0x80 0x80 0x80 0x00 0x00 
: car15 0x80 0x80 0x80 0x80 0x80 0x80 0x00 0x00 
: car11 0x80 0x00 0x00 0x80 0x80 0x00 0x00 0x00 
: car12 0x80 0x00 0x00 0x80 0x80 0x00 0x80 0x80 
: car13 0x80 0x00 0x00 0x80 0x80 0x00 0x80 0x80 
: car14 0x80 0x80 0x80 0x80 0x80 0x00 0x00 0x00 
: car16 0x00 0x80 0x80 0x80 0x80 0x80 0x00 0x00 

: titleup1
0x00 0x00 0x00 0x00 0x7D 0xE9 0x55 0x29 
0x11 0x69 0x11 0x49 0x11 0x26 0x00 0x00 
0x7F 0xFF 0x00 0x00 0x00 0x00 0x01 0x99 
0x01 0x55 0x01 0x59 0x01 0x95 0x00 0x00 
: titleup2
0x00 0x00 0x00 0x00 0x3A 0x4E 0x42 0x90 
0x43 0x0C 0x42 0x82 0x3A 0x5C 0x00 0x00 
0xFF 0xFF 0x00 0x00 0x00 0x00 0x57 0x40 
0x54 0x40 0x56 0x00 0x27 0x40 0x00 0x00 
: titleup3
0x00 0x00 0x00 0x00 0xA2 0x94 0xB6 0x94 
0xAA 0x94 0xA2 0x94 0xA2 0x67 0x00 0x00 
0xFF 0xFF 0x00 0x00 0x00 0x00 0x00 0x00 
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
: titleup4
0x00 0x00 0x00 0x00 0x31 0x9E 0x4A 0x52 
0x32 0x56 0x4A 0x54 0x31 0x92 0x00 0x00 
0xFF 0xFE 0x00 0x00 0x00 0x00 0x00 0x00 
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
: titledown1
0x6A 0xEE 0x8A 0x84 0x8A 0x24 0x6E 0xE4 
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
: titledown2
0xEA 0xBB 0xAE 0x92 0xAA 0xA3 0xEA 0xBB 
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
: titledown3
0x80 0x00 0x00 0x00 0x00 0x00 0x80 0x00 
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
: titlecursor
0xC0 0x20 0x20 0xC0 0x00 0x00 0x00 0x00 

: customizeup1
0x00 0x00 0x70 0x00 0x45 0x77 0x45 0x42 
0x45 0x72 0x45 0x12 0x77 0x72 0x00 0x00 
0x00 0x00 0x00 0x00 0x0C 0xEC 0x0A 0x8A 
0x0C 0xCA 0x0A 0xEC 0x00 0x00 0x0C 0xEC 
: customizeup2
0x00 0x00 0x00 0x00 0x75 0x5D 0x55 0x45 
0x57 0x49 0x57 0x51 0x75 0x5D 0x00 0x00 
0x00 0x00 0x00 0x00 0x32 0xAE 0x2A 0xE8 
0x32 0xA2 0x2A 0xAE 0x00 0x00 0xA3 0x2A 
: customizeup3
0x00 0x00 0x00 0x00 0xC0 0x00 0x00 0x00 
0xC0 0x00 0x00 0x00 0xC0 0x00 0x00 0x00 
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
0x00 0x00 0x00 0x00 0x00 0x00 0xE0 0x00 
: customizedown1
0xAA 0xAC 0xAE 0xCA 0xCA 0xAA 0x00 0x00 
0xAE 0x8A 0xAA 0xCC 0xEA 0xAA 0xAE 0xAA 
0x00 0x00 0x8E 0xEA 0xCA 0x8C 0xAE 0x8A 
0xEA 0xEA 0x00 0x00 0x00 0x00 0x00 0x00 
: customizedown2
0x2A 0xE8 0x32 0xA2 0x2A 0xAE 0x00 0x00 
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 

: bigdude
0x3F 0x00 0x3F 0x00 0x3F 0x00 0x3F 0x00 
0x0C 0x00 0x0C 0x00 0x3F 0x00 0x3F 0x00 
0x3C 0xC0 0x3C 0xC0 0x3C 0x00 0x3C 0x00 
0x33 0x00 0x33 0x00 0xC3 0x00 0xC3 0x00 
: bigdudeshoes
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
0x00 0x00 0x00 0x00 0xC3 0x00 0xC3 0x00 
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
0x00 0x00 0x00 0x00 0xC3 0x00 0xC3 0x00 

: honk1
0x7F 0xFF 0x80 0x00 0xA8 0x04 0xA8 0x05 
0xBB 0x66 0xAB 0x55 0x80 0x00 0x71 0xFF 
0x0A 0x00 0x14 0x00 0x38 0x00 0x00 0x00 
0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
: honk2
0x80 0x40 0x40 0x40 0x40 0x40 0x40 0x80 

: has_honk
	i := honk
	load v0
;

: toggle_honk
	has_honk
	v1 := 1
	v0 ^= v1
	i := honk
	save v0
;

: set_tires
	i := tires
	save v0
;

: get_tires
	i := tires
	load v0
;

: reset_score
	i := score
	v0 := 0
	v1 := 0
	v2 := 0
	save v1
;
: increment_score
	i := score
	load v2
	v0 += 1
	if v0 == 0 begin
		v1 += 1
		if v1 == 0 then
			v2 += 1
	end
	i := score
	save v1
;

: truckdir -1
: truckpos 0 20

: spawn_truck
	i := truckdir
	v0 := random 1
	v1 := 0
	if v0 == 0 begin
		v0 := -1
		v1 := 63
	end

	i := truckdir
	save v0

	v0 := v1 
	v1 := 10
	v3 := random 1
	if v3 == 0 begin
		v1 := 20
	end
	
	i := truckpos
	save v1	
;

: move_truck_to_top
	i := truckpos
	load v1
	v1 := 10
	i := truckpos
	save v1
;

: should_spawn_new_truck
	i := truckdir
	load v0
	v3 := v0
	i := truckpos
	load v1
	
	if v3 == -1 begin
		if v0 == -24 begin
			v0 := 1
		else 
			v0 := 0
		end		
	end
	if v3 == 1 begin
		if v0 == 87 begin
			v0 := 1
		else
			v0 := 0
		end
		
	end	
;

: handleinput
    v2 := 5 # up
    if v2 key then
        vd := 10

    v2 := 8 # down
    if v2 key then
	vd := 20
;

: move
	v0 := 1
	vb -= v0
	v1 := 31
	
	v1 &= vb
	if v1 == 31 then
		va -= v0

	should_spawn_new_truck
	if v0 == 1 then
		spawn_truck

	i := truckdir
	load v0
	v3 := v0

	i := truckpos
	load v1
	v4 := 1
	if v3 == 1 begin
		v0 += v4
	else
		v0 -= v4
	end
	i := truckpos
	save v1

	check_for_death
	if v0 == 1 begin
		v0 := 100
		buzzer := v0
		you_lose_screen
		v9 := 1
	end	
;

: you_lose_screen
	cool_clear
	v0 := 21
	v1 := 9
	i := c
	sprite v0 v1 4
	v0 += 4
	i := r
	sprite v0 v1 4
	v0 += 4
	i := a
	sprite v0 v1 4
	v0 += 4
	i := s
	sprite v0 v1 4
	v0 += 4
	i := h
	sprite v0 v1 4
	v0 += 4
	i := bang
	sprite v0 v1 4


	i := score
	load v2
	v5 := 32
	v6 := 19
	v4 := v0
	draw_score_part
	v4 := v1
	v5 += 236
	draw_score_part

	v0 := key
	v0 := key
;

: check_for_death
	i := truckpos 
	load v1 # v0=x v1=y
	# vd == dude-lane
	# vc == dudex
	v3 := 0
	if vd == v1 begin
		if v0 >= 23 begin
			if v0 <= 25 then
				v3 := 1
		end
	end
	v0 := v3	
;

: draw_car_part
	if v5 < 64 then
		sprite v5 v1 8
	v5 += v4	
	i += v3
;

: draw_car
	i := truckdir
	load v0
	v6 := v0

	i := truckpos
	load v1
	v1 += 3
	v5 := v0
	i := car1
	v3 := 8

	if v6 == 1 begin
		v4 := -1
	else
		v4 := 1
	end

	draw_car_part
	draw_car_part
	draw_car_part
	draw_car_part
	draw_car_part
	draw_car_part
	draw_car_part
	draw_car_part
	draw_car_part
	draw_car_part
	draw_car_part
	draw_car_part
	draw_car_part
	draw_car_part
	draw_car_part
	draw_car_part
;

: draw_pixel
	i := pixel
	sprite v0 v1 1
	if vf != 0 then
		sprite v0 v1 1
;

: draw
	plane 2
	clear
	plane 1
	clear

	draw_car
	
	# background

	i := topline
	v1 := 12
	v0 := 0 	sprite v0 v1 8
	v0 := 8		sprite v0 v1 8
	v0 := 16	sprite v0 v1 8
	v0 := 24	sprite v0 v1 8
	v0 := 32	sprite v0 v1 8
	v0 := 40	sprite v0 v1 8
	v0 := 48	sprite v0 v1 8
	v0 := 56	sprite v0 v1 8

	i := topline
	v0 := vb
	v0 >>= v0
	v0 >>= v0
	v1 := 22
	sprite v0 v1 1
	v0 += 22
	sprite v0 v1 1
	v0 += 22
	sprite v0 v1 1

	i := mountain
	v0 := vb
	v0 >>= v0
	v0 >>= v0
	v1 := 4
	sprite v0 v1 8

	i := house
	v0 += 20
	v1 := 4
	sprite v0 v1 8

	i := church
	v0 += 15
	v1 := 4
	sprite v0 v1 8

	i := tree
	v0 += 12
	v1 := 4
	sprite v0 v1 8

	# Unrolled for speeeeeds.
	v0 := va v1 := 8 draw_pixel
	v0 += 1 v1 := 7 draw_pixel
	v0 += 1 v1 := 6 draw_pixel
	v0 += 1 v1 := 5 draw_pixel
	v0 += 1 v1 := 4 draw_pixel
	v0 += 1 v1 := 5 draw_pixel
	v0 += 1 v1 := 6 draw_pixel
	v0 += 1 v1 := 7 draw_pixel
	v0 += 1 v1 := 8 draw_pixel
	v0 += 1 v1 := 8 draw_pixel
	v0 += 1 v1 := 7 draw_pixel
	v0 += 1 v1 := 6 draw_pixel
	v0 += 1 v1 := 5 draw_pixel
	v0 += 1 v1 := 4 draw_pixel
	v0 += 1 v1 := 3 draw_pixel
	v0 += 1 v1 := 2 draw_pixel
	v0 += 1 v1 := 1 draw_pixel
	v0 += 1 v1 := 2 draw_pixel
	v0 += 1 v1 := 3 draw_pixel
	v0 += 1 v1 := 4 draw_pixel
	v0 += 1 v1 := 5 draw_pixel
	v0 += 1 v1 := 6 draw_pixel
	v0 += 1 v1 := 7 draw_pixel
	v0 += 1 v1 := 8 draw_pixel
	v0 += 1 v1 := 8 draw_pixel
	v0 += 1 v1 := 7 draw_pixel
	v0 += 1 v1 := 6 draw_pixel
	v0 += 1 v1 := 5 draw_pixel
	v0 += 1 v1 := 4 draw_pixel
	v0 += 1 v1 := 5 draw_pixel
	v0 += 1 v1 := 6 draw_pixel
	v0 += 1 v1 := 7 draw_pixel
	v0 += 1 v1 := 8 draw_pixel
	v0 += 1 v1 := 8 draw_pixel
	v0 += 1 v1 := 7 draw_pixel
	v0 += 1 v1 := 6 draw_pixel
	v0 += 1 v1 := 5 draw_pixel
	v0 += 1 v1 := 4 draw_pixel
	v0 += 1 v1 := 3 draw_pixel
	v0 += 1 v1 := 2 draw_pixel
	v0 += 1 v1 := 3 draw_pixel
	v0 += 1 v1 := 4 draw_pixel
	v0 += 1 v1 := 5 draw_pixel
	v0 += 1 v1 := 6 draw_pixel
	v0 += 1 v1 := 7 draw_pixel
	v0 += 1 v1 := 8 draw_pixel
	v0 += 1 v1 := 8 draw_pixel
	v0 += 1 v1 := 7 draw_pixel
	v0 += 1 v1 := 6 draw_pixel
	v0 += 1 v1 := 5 draw_pixel
	v0 += 1 v1 := 4 draw_pixel
	v0 += 1 v1 := 5 draw_pixel
	v0 += 1 v1 := 6 draw_pixel
	v0 += 1 v1 := 7 draw_pixel
	v0 += 1 v1 := 8 draw_pixel
	v0 += 1 v1 := 8 draw_pixel
	v0 += 1 v1 := 7 draw_pixel
	v0 += 1 v1 := 6 draw_pixel
	v0 += 1 v1 := 5 draw_pixel
	v0 += 1 v1 := 6 draw_pixel
	v0 += 1 v1 := 7 draw_pixel
	v0 += 1 v1 := 8 draw_pixel
	v0 += 1 v1 := 7 draw_pixel
	v0 += 1 v1 := 8 draw_pixel

	# dude
	v4 := 2
	v3 := vb
	v3 &= v4
	if v3 == 0 begin
		i := dude
		sprite vc vd 8
		i := dudeshoes
		sprite vc vd 8
		get_tires
		if v0 == 0 then plane 0
		if v0 == 1 then plane 1
		if v0 == 2 then plane 2
		if v0 == 3 then plane 3
		i := dudeshoes
		sprite vc vd 8
		plane 1
	else
		i := dude2
		sprite vc vd 8
		i := dudeshoes2
		sprite vc vd 8
		get_tires
		if v0 == 0 then plane 0
		if v0 == 1 then plane 1
		if v0 == 2 then plane 2
		if v0 == 3 then plane 3
		i := dudeshoes2
		sprite vc vd 8
		plane 1	end
	plane 1

	# honk
	has_honk
	if v0 == 1 begin
		v0 := vb
		v1 := 32
		v0 &= v1
	
		if v0 > 0 begin
			i := honk1
			v0 := vc
			v1 := vd
			v0 += 8
			v1 += 246
			sprite v0 v1 0
			i := honk2
			v0 += 16
			sprite v0 v1 8
		end
	end
	
	draw_score
;

: draw_number # x=v5, y=v6, num=v7
	i := numbers
	v7 <<= v7
	v7 <<= v7
	i += v7
	sprite v5 v6 4 
;

: draw_score_part
	v7 := 0
	loop
		if v4 >= 100 begin
			v7 += 1
			v4 += 156
		end
		if v4 >= 100 then
	again
	draw_number
	v7 := 0
	v5 += 4
	loop
		if v4 >= 10 begin
			v7 += 1
			v4 += 246
		end
		if v4 >= 10 then
	again
	draw_number
	v7 := 0
	v5 += 4
	loop
		if v4 >= 1 begin
			v7 += 1
			v4 += 255
		end
		if v4 >= 1 then
	again
	draw_number
;

: draw_score
	i := score
	load v2
	v5 := 52
	v6 := 0
	v4 := v0
	draw_score_part
	v4 := v1
	v5 += 236
	draw_score_part
;

: cool_clear
	plane 2
	clear
	plane 1

	v2 := 0
	v3 := 0
	loop
		v0 := random 63
		v1 := random 31
		i := pixel
		sprite v0 v1 1
		if vf == 0 then
			sprite v0 v1 4

		v2 += 1
		if v2 == 0 then
			v3 += 1
		if v3 != 40 then
	again
	clear
;

: mainscreen
	plane 3
	clear
	plane 1

	i := titleup1
	v0 := 0
	v1 := 0
	sprite v0 v1 0
	i := titleup2
	v0 += 16
	sprite v0 v1 0
	i := titleup3
	v0 += 16
	sprite v0 v1 0
	i := titleup4
	v0 += 16
	sprite v0 v1 0

	i := titledown1
	v0 := 7
	v1 := 16
	sprite v0 v1 0
	i := titledown2
	v0 += 16
	sprite v0 v1 0
	i := titledown3
	v0 += 16
	sprite v0 v1 0

	v3 := 11
	loop
		i := titlecursor
		v0 := 2
		sprite v0 v3 4
		if vf != 0 begin
			sprite v0 v3 4
		end

		v2 := key

		if v2 == 5 begin #up
			if v3 != 11 begin
				sprite v0 v3 4
				v3 += -5
			end
		end

		if v2 == 8 begin
			if v3 != 26 begin
				sprite v0 v3 4
				v3 += 5
			end
		end
		if v2 != 6 then
	again

	if v3 == 11 begin
		game
	end
	if v3 == 16 begin
		customize
	end
;

: customize
	v3 := 10
	loop
		plane 3
		clear
		plane 1
		i := customizeup1
		v0 := 0
		v1 := 0
		sprite v0 v1 0
		i := customizeup2
		v0 += 16
		sprite v0 v1 0
		i := customizeup3
		v0 += 16
		sprite v0 v1 0
		
		i := customizedown1
		v0 := 4
		v1 := 16
		sprite v0 v1 0
		i := customizedown2
		v0 += 16
		sprite v0 v1 0

		i := bigdude
		v0 := 36
		v1 := 10
		sprite v0 v1 0
		i := bigdudeshoes
		v0 := 36
		v1 := 10
		sprite v0 v1 0

		get_tires
		if v0 == 0 then plane 0
		if v0 == 1 then plane 1
		if v0 == 2 then plane 2
		if v0 == 3 then plane 3
		i := bigdudeshoes
		v0 := 36
		v1 := 10
		sprite v0 v1 0
		plane 1

		has_honk
		if v0 == 1 begin
			i := honk1
			v0 := 45
			v1 := 2
			sprite v0 v1 0
			i := honk2
			v0 += 16
			sprite v0 v1 8
		end

		i := titlecursor
		v0 := 0
		sprite v0 v3 4
		if vf != 0 begin
			sprite v0 v3 4
		end

		v2 := key

		if v2 == 5 begin #up
			if v3 != 10 begin
				sprite v0 v3 4
				v3 += -5
			end
		end

		if v2 == 8 begin
			if v3 != 25 begin
				sprite v0 v3 4
				v3 += 5
			end
		end
		
		v5 := 1
		if v2 == 6 begin
			if v3 == 10 begin
				get_tires
				if v0 != 1 begin
					v0 := 1
					set_tires
				else
					v0 := 2
					set_tires
				end
			end
			if v3 == 15 begin
				get_tires
				if v0 != 1 begin
					v0 := 1
					set_tires
				else
					v0 := 3
					set_tires
				end
			end
			if v3 == 20 begin
				toggle_honk
			end
			if v3 == 25 begin
				v5 := 0
			end
		end
		if v5 == 1 then
	again
;

: game
	vb := 255
	va := 255

	vc := 20
	vd := 20
	reset_score
	spawn_truck
	move_truck_to_top
	loop		
		handleinput
		move
		if v9 == 0 begin
			draw
			increment_score
			sync
		end
		if v9 == 0 then
	again
	v9 := 0
;

: main
	loop
		mainscreen
	again
;

: sync
    loop
        vf := delay
        if vf != 0 then
    again

    vf := 1
    delay := vf
;
